import 'whatwg-fetch'
export const SET_CENTER = 'SET_CENTER'
export const WORK_BERTH = 'WORK_BERTH' //
export const TODAY_ORDER = 'TODAY_ORDER' //
export const TODAY_TALLYING = 'TODAY_TALLYING' //
export const TEMPER_DATA = 'TEMPER_DATA' //
export const WORLD_ORDER = 'WORLD_ORDER' //
export const CHINA_ORDER = 'CHINA_ORDER' //
export const CHINA_LINE = 'CHINA_LINE' //
export const ORDER_RATE = 'ORDER_RATE' //
export const ORDER_PUNCTUALITY = 'ORDER_PUNCTUALITY' //
export const PRODUCT_DAILY = 'PRODUCT_DAILY' //
export const PRODUCT_LOW = 'PRODUCT_LOW' //
export const SCHEDULING = 'SCHEDULING' //
export const SCHEDULING_STAT = 'SCHEDULING_STAT' //
export const ORDER_LIST = 'ORDER_LIST' //
export const INVENTORY = 'INVENTORY' //
export const SIGNAL_SPY = 'SIGNAL_SPY' //
export const SIGNAL_SPY_WEEK = 'SIGNAL_SPY_WEEK' //
export const SIGNAL_SPY_ALL = 'SIGNAL_SPY_ALL' //
export const COMPLETE_SPY = 'COMPLETE_SPY' //
export const COMPLETE_SPY_WEEK = 'COMPLETE_SPY_WEEK' //
export const COMPLETE_SPY_ALL = 'COMPLETE_SPY_ALL' //
export const TOTAL_SPY = 'TOTAL_SPY' //
export const TOTAL_SPY_ALL = 'TOTAL_SPY_ALL' //
export const DEVICE_LIST = 'DEVICE_LIST' //
export const CHECK_MONTH = 'CHECK_MONTH' //
export const CHECK_DAY = 'CHECK_DAY' //
export const EXCEPTION_LIST = 'EXCEPTION_LIST' //
export const EXCEPTION_CYCLE = 'EXCEPTION_CYCLE' //
export const EXCEPTION_CLOSE = 'EXCEPTION_CLOSE' //
export const DELIVERY_COUNT = 'DELIVERY_COUNT' //
export const SIGNAL_SPY_TIME = 'SIGNAL_SPY_TIME' //
export const COMPLETE_SPY_TIME = 'COMPLETE_SPY_TIME' //
export const PRODUCTION_COUNT = 'PRODUCTION_COUNT' //
export const REFLOW = 'REFLOW' //
export const PRESS = 'PRESS' //
export const SPI = 'SPI' //
export const CLIP = 'CLIP' //
export const COMPLETE_SPY_TIME_ALL = 'COMPLETE_SPY_TIME_ALL' //
export const SIGNAL_SPY_TIME_ALL = 'SIGNAL_SPY_TIME_ALL' //
export const EXCEPTION_STAT = 'EXCEPTION_STAT' //
export const AOI = 'AOI' //
export const WELDER = 'WELDER' //
export const WAVE = 'WAVE' //
export const AXI = 'AXI' //
export const CHANGE_KEY = 'CHANGE_KEY' //
export const FILTER_TABLE = 'FILTER_TABLE' //

export function setCenter (center) {
    return {
        type: SET_CENTER,
        center
    }
}
export function changeKey () {
    return {
        type: CHANGE_KEY
    }
}

function loadData (type, data) {
    return {
        type,
        data
    }
}

function getRequest (url, param, method) {
    const keys = Object.keys(param)
    let body = ''
    keys.forEach((key) => {

        if (typeof param[key] === 'object') {
            body += `${key}=${JSON.stringify(param[key])}&`
        } else {
            body += `${key}=${param[key]}&`
        }
    })

    body = body.substring(0, body.length - 1)
    switch (method) {
        case 'get':
            body.length && (body = `?${body}`)
            return new Request(`${url + body}`, {
                method: 'GET',
                mode: 'no-cors',
                'content-type': 'application/json;charset=UTF-8'
            })
        case 'post':
            return new Request(`${url}`, {
                method: 'POST',
                headers: {
                    'Accept': 'application/json, text/javascript, */*; q=0.01',
                    'Content-Type': 'application/json; charset=UTF-8'
                },
                body
            })
    }

}

function asyncData (request, type) {
    return function (dispatch) {
        fetch(request).then(
            function (res) {
                if (res.ok) {
                    res.json().then(function (data) {
                        dispatch(loadData(type, data))
                    })
                } else {
                }
            }, function (e) {
                console.log('error', e)
            }
        )
    }
}
/*获取今日订单看板数据*/
export function getTodayOrder (param = {}) {
    const request = getRequest('/api/h3c/order/progress/today', param, 'get')
    return asyncData(request, TODAY_ORDER)
}

/*获取今日订单备货监控图*/
export function getTodayTallying (param = {}) {
    const request = getRequest('/api/h3c/order/progress/stats', param, 'get')
    return asyncData(request, TODAY_TALLYING)
}

/*获取温湿度信息*/
export function getTemper (param = {}) {
    const request = getRequest('/api/h3c/cm/sensor/metrics', param, 'get')
    return asyncData(request, TEMPER_DATA)
}

/*全球发货*/
export function getWorldOrder (param = {}) {
    const request = getRequest('/api/h3c/order/percent/world', param, 'get')
    return asyncData(request, WORLD_ORDER)
}

/*国内发货*/
export function getChinaOrder (param = {}) {
    const request = getRequest('/api/h3c/order/percent/province', param, 'get')
    return asyncData(request, CHINA_ORDER)
}

/*国内发货线图*/
export function getChinaLine (param = {}) {
    const request = getRequest('/api/h3c/order/delivery/city', param, 'get')
    return asyncData(request, CHINA_LINE)
}

/*交付率*/
export function getRate (param = {}) {
    const request = getRequest('/api/h3c/order/delivery/rate', param, 'get')
    return asyncData(request, ORDER_RATE)
}
/*正点率*/
export function getPunctuality (param = {}) {
    const request = getRequest('/api/h3c/order/delivery/punctuality', param, 'get')
    return asyncData(request, ORDER_PUNCTUALITY)
}

/*每日库存需求满足率*/
export function getProductDaily (param = {}) {
    const request = getRequest('/api/h3c/prod/fillrate/daily', param, 'get')
    return asyncData(request, PRODUCT_DAILY)
}

/*最低库存需求满足率*/
export function getProductLow (param = {}) {
    const request = getRequest('/api/h3c/prod/fillrate/low', param, 'get')
    return asyncData(request, PRODUCT_LOW)
}

/*排产计划*/
export function getScheduling (param = {}) {
    const request = getRequest(`/api/h3c/prod/scheduling/${param.factoryName}`, {}, 'get')
    return asyncData(request, SCHEDULING)
}

/*排产计划*/
export function getSchedulingStat (param = {}) {
    const request = getRequest(`/api/h3c/prod/scheduling/${param.factoryName}/stats`, param, 'get')
    return asyncData(request, SCHEDULING_STAT)
}

/*订单列表*/
export function getOrderList (param = {}) {
    const request = getRequest(`/api/h3c/order/list`, param, 'get')
    return asyncData(request, ORDER_LIST)
}

/*订单筛选*/
export function filterOrder (param = {}) {
    return {
        type: FILTER_TABLE,
        param
    }
}

/*订单列表*/
export function getInventory (param = {}) {
    const request = getRequest(`/api/h3c/prod/inventory/info`, param, 'get')
    return asyncData(request, INVENTORY)
}

/*合格率*/
export function getFpy (param = {}) {
    const request = getRequest(`/api/h3c/quality/fpy`, {...param, type: encodeURI(param.type)}, 'get')
    switch (param.type) {
        case '单板':
            return asyncData(request, SIGNAL_SPY)
        case '整机':
            return asyncData(request, COMPLETE_SPY)
        case '综合':
            return asyncData(request, TOTAL_SPY)
        case '单板全部':
            return asyncData(request, SIGNAL_SPY_ALL)
        case '整机全部':
            return asyncData(request, COMPLETE_SPY_ALL)
        case '综合全部':
            return asyncData(request, TOTAL_SPY_ALL)
        case '周单板':
            return asyncData(request, SIGNAL_SPY_WEEK)
        case '周整机':
            return asyncData(request, COMPLETE_SPY_WEEK)
        case '单板实时':
            return asyncData(request, SIGNAL_SPY_TIME)
        case '整机实时':
            return asyncData(request, COMPLETE_SPY_TIME)
        case '单板实时全部':
            return asyncData(request, SIGNAL_SPY_TIME_ALL)
        case '整机实时全部':
            return asyncData(request, COMPLETE_SPY_TIME_ALL)
    }
}

/*设备列表*/
export function getDevice (param = {}) {
    const request = getRequest(`/api/h3c/cm/device`, param, 'get')
    return asyncData(request, DEVICE_LIST)
}

/*检查合格率趋势图*/
export function getCheckRate (param = {}) {
    const request = getRequest(`/api/h3c/quality/pass/rate`, param, 'get')
    switch (param.type) {
        case 'day':
            return asyncData(request, CHECK_DAY)
        case 'month':
            return asyncData(request, CHECK_MONTH)
    }
}

/*检查合格率趋势图*/
export function getExceptionList (param = {}) {
    const request = getRequest(`/api/h3c/exception/list`, param, 'get')
    return asyncData(request, EXCEPTION_LIST)
}

/**/
export function getExceptionCycle (param = {}) {
    const request = getRequest(`/api/h3c/exception/stats/period`, param, 'get')
    return asyncData(request, EXCEPTION_CYCLE)
}

/*问题单及时关闭率*/
export function getExceptionClose (param = {}) {
    const request = getRequest(`/api/h3c/exception/stats/week`, param, 'get')
    return asyncData(request, EXCEPTION_CLOSE)
}

/**/
export function getDeliveryCount (param = {}) {
    const request = getRequest(`/api/h3c/order/delivery/stats`, param, 'get')
    return asyncData(request, DELIVERY_COUNT)
}

/**/
export function getProductionCount (param = {}) {
    const request = getRequest(`/api/h3c/prod/production/stats`, param, 'get')
    return asyncData(request, PRODUCTION_COUNT)
}

/**/
export function getDeviceMetrics (param = {}) {
    // const request = getRequest(`/api/h3c/prod/production/stats`, param, 'get')
    const request = getRequest(`/api/h3c/cm/device/${param.deviceId}/metrics`, {}, 'get')
    switch (param.type) {
        case '印刷机':
            return asyncData(request, PRESS)
        case 'SPI':
            return asyncData(request, SPI)
        case '贴片机':
            return asyncData(request, CLIP)
        case '回流炉':
            return asyncData(request, REFLOW)
        case 'AOI':
            return asyncData(request, AOI)
        case '伺服压接机':
            return asyncData(request, WELDER)
        case '选择性波峰焊':
            return asyncData(request, WAVE)
        case 'AXI':
            return asyncData(request, AXI)

    }
}

/**/
export function getExceptionStat (param = {}) {
    const request = getRequest(`/api/h3c/exception/stats`, {}, 'get')
    return asyncData(request, EXCEPTION_STAT)
}







